package com.itshizhan.coroutinedemobasic

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import kotlinx.coroutines.*

class MainActivity : AppCompatActivity() {
    private val scope = MainScope()

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        /*
        val job = GlobalScope.launch(Dispatchers.Main) {
            val content = fetchData()
            Log.d("itshizhan","获取的数据为: $content")

             //当前在Main主线程，如果需要切换线程，使用 withContext
             //withContext(Dispatchers.IO) { }

        }
        job.start()
         */

        val job = CoroutineScope(Dispatchers.Main).launch {
            val content = fetchData()
            Log.d("itshizhan","获取的数据为: $content")

        }
        job.start()


    }

   suspend fun fetchData(): String {
       delay(1000L)
       Log.d("itshizhan","正在获取数据")
       return "hello coroutine"
    }
}

